package com.ruoyi.kjy.controller;

/**
 * @Description:
 * @Author: Hi，Wang
 * @Date: 11/10,0010 20:07
 */


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.kjy.domain.*;
import com.ruoyi.kjy.service.CreateSpaceService;
import com.ruoyi.kjy.service.RoomReservationService;
import com.ruoyi.kjy.service.RoomsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * @Description:
 * @Author: Hi，Wang
 * @Date: 10/10,0010 20:19
 */

@RestController
@RequestMapping("/rooms")
public class RoomsController extends BaseController {


    @Autowired
    private RoomsService roomsService;


    @Autowired
    private RoomReservationService roomReservationService;

    @GetMapping("/list")
    public AjaxResult list(Long current, Long size, Rooms rooms) {
        QueryWrapper<Rooms> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(rooms);
        if (Objects.isNull(current)) {
            current = 1l;
        }
        if (Objects.isNull(size)) {
            size = 10l;
        }
        if (size >= 20l) {
            size = 20l;
        }

        // TODO 很慢
        Page<Rooms> page = roomsService.page(new Page<>(current, size), queryWrapper);
        for (Rooms room : page.getRecords()){
            QueryWrapper<RoomReservation> queryWrapper1= new QueryWrapper<>();
            queryWrapper1.eq("room_id", room.getId());
            List<RoomReservation> list = roomReservationService.list(queryWrapper1);
            room.setRoomReservations(list);

        }
        return AjaxResult.success(page);
    }




    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        Rooms rooms = roomsService.getById(id);
        QueryWrapper<RoomReservation> queryWrapper1= new QueryWrapper<>();
        queryWrapper1.orderByDesc("update_time");
        queryWrapper1.eq("room_id", rooms.getId());
        List<RoomReservation> list = roomReservationService.list(queryWrapper1);
        rooms.setRoomReservations(list);
        return AjaxResult.success(rooms);
    }


    @Log(title = "会议室", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Rooms rooms) {
        rooms.setStatus(0);
        boolean save = roomsService.save(rooms);
        return toAjax(save);
    }

    @Log(title = "会议室", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Rooms rooms) {
        return toAjax(roomsService.updateById(rooms));
    }

    @Log(title = "众创空间", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(roomsService.removeByIds(Arrays.asList(ids)));
    }








}
